home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / SITES / RAND / DVIEW030.LZH / VIEWMISC.C < prev   
Encoding:
C/C++ Source or Header  |  1995-09-10  |  7.4 KB  |  342 lines

  1. #define C
  2.  
  3. #undef PROFILE
  4. #define COUNT
  5.  
  6. #include "view.h"
  7. #include "trig.h"
  8.  
  9. #ifdef __GNUC__
  10.   #include <osbind.h>
  11.   #include <memory.h>
  12. #else
  13.   #include <tos.h>
  14. #endif
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.             
  20. #define SKY_COLOR       1
  21. #define FLOOR_COLOR     2
  22. #define WALL_COLOR      3
  23. #define RED_COLOR       4
  24. #define LEDGE_COLOR     5
  25. #define ZMIN            20L  
  26.  
  27. #define UPPER_TYPE      0
  28. #define WALL_TYPE       1
  29. #define LOWER_TYPE      2
  30.  
  31. #ifdef C
  32. void DrawNode(short node_num);
  33. void DrawSegs(void);
  34. #endif
  35.  
  36. short width = 320;
  37. short height = 200;
  38. short scale = 1;
  39.  
  40. short flooropt = 0;
  41. short floorcol = 0;
  42. short nofloor = 0;
  43. short wallopt = 0;
  44. short wallcol = 0;
  45. short singlestep = 0;
  46. short showdata = 0;
  47. short draw_2D = 0;
  48. short chksect = 1;
  49. short tellsect = 0;
  50.  
  51. #ifdef COUNT
  52. long drawview;
  53.  
  54. long addfloor;
  55. long addfloor_loops;
  56.  
  57. long addwall;
  58. long addwall_loops;
  59.  
  60. long coldraw;
  61. long coldraw_loops;
  62.  
  63. long rowdraw;
  64. long rowdraw_loops;
  65.  
  66. long drawnode;
  67. long drawssector;
  68. long drawssector_loops;
  69. long loadseg;
  70. long loadseg_loops1;
  71. long loadseg_loops2;
  72.  
  73. long setuptime;
  74. long drawtime;
  75. long c2ptime;
  76. #endif
  77.  
  78. /* These are some yucky global variables.  They should probably
  79.  * be moved into the class's member data.
  80.  */
  81. short ColCount;
  82. short WallCount;
  83. short WallRunCount;
  84. short FirstSSector;
  85.  
  86. extern short MaxNode;
  87.  
  88. /* Elements of this array indicate if a screen column is completely drawn. */
  89.  
  90. #if 0
  91. short     Col_Done[320];
  92. #endif
  93. char      Col_Done[320];    /* Slightly better for the cache */
  94.  
  95.  
  96. /* Elements of this array hold indexes into the wall_run array. */
  97.  
  98. #if 1
  99. short     intersections[50][320];
  100. #endif
  101.  
  102. /* The number of wall_runs visible on a particular screen column. */
  103.  
  104. #if 1
  105. short     int_count[320];
  106. #endif
  107.  
  108. /* MaxY & MinY are the active edge lists for the top & bottom of the screen. */
  109.  
  110. short     MaxY[320];
  111. short     MinY[320];
  112.  
  113.  
  114. /* This is the wall_run array.  It contains all of the wall_runs which
  115.  * are visible in a single frame.
  116.  */
  117.  
  118. #if 1
  119. wall_run  walls[8000];  /* 320*50 = 16000 */
  120. #endif
  121.  
  122. short walltop[320];
  123. short wallbottom[320];
  124.  
  125. /* The next two arrays are used for both floors and ceilings.
  126.  * Elements of this array hold indexes into the floor_run array.
  127.  */
  128.  
  129. #if 1
  130. floor_run floorlist[200][40];
  131. #endif
  132.  
  133. short floorlst[200];
  134. short floortex[200];
  135.  
  136. /* The number of floor_runs visible on a particular screen column. */
  137.  
  138. short     runcount[200];
  139.  
  140.  
  141. /* The offscreen buffer. */
  142. char *screenbuf;
  143.  
  144.  
  145. extern void c2p(char *buf, int w_area, int h_area);
  146. extern long get_timer(void);
  147.  
  148.  
  149. /* This is the constructor for the View. */
  150.  
  151. void ViewSetup(void)
  152. {
  153. #ifdef COUNT
  154.    drawview = 0;
  155.    addfloor       = 0;
  156.    addfloor_loops = 0;
  157.    addwall        = 0;
  158.    addwall_loops  = 0;
  159.    coldraw        = 0;
  160.    coldraw_loops  = 0;
  161.    rowdraw        = 0;
  162.    rowdraw_loops  = 0;
  163.    drawnode = 0;
  164.    drawssector = 0;
  165.    drawssector_loops = 0;
  166.    loadseg = 0;
  167.    loadseg_loops1 = 0;
  168.    loadseg_loops2 = 0;
  169.    setuptime = 0;
  170.    drawtime = 0;
  171.    c2ptime = 0;
  172. #endif
  173.  
  174.    Seg_Array       = 0;
  175.    Side_Array      = 0;
  176.    Line_Array      = 0;
  177.    Node_Array      = 0;
  178.    PNode_Array     = 0;
  179.    Sector_Array    = 0;
  180.    Vertex_Array    = 0;
  181.    SSector_Array   = 0;
  182.    Blockmap_Array  = 0;
  183.    Blockmap_Header = 0;
  184.  
  185.    if ((screenbuf = malloc(64000L * sizeof(char))) == NULL)
  186.       exit(-1);
  187. }
  188.  
  189.  
  190. /* The destructor deletes all of the dynamically allocated memory. */
  191.  
  192. void ViewRemove(void)
  193. {
  194. #ifdef COUNT
  195.    printf("\nDrawview: %ld\n", drawview);
  196.    printf("Addfloor: %ld %ld\n", addfloor / drawview, addfloor_loops / drawview);
  197.    printf("Addwall:  %ld %ld\n", addwall / drawview, addwall_loops / drawview);
  198.    printf("Coldraw:  %ld %ld\n", coldraw / drawview, coldraw_loops / drawview);
  199.    printf("Rowdraw:  %ld %ld\n", rowdraw / drawview, rowdraw_loops / drawview);
  200.    printf("Drawnode:     %ld\n", drawnode / drawview);
  201.    printf("Drawssector:  %ld %ld\n", drawssector / drawview, drawssector_loops / drawview);
  202.    printf("Loadseg:      %ld %ld %ld\n", loadseg / drawview, loadseg_loops1 / drawview, loadseg_loops2 / drawview);
  203.    printf("Setup time:   %ld\n", setuptime / drawview);
  204.    printf("Draw time:    %ld\n", drawtime / drawview);
  205.    printf("C2p time:     %ld\n", c2ptime / drawview);
  206. #endif
  207.  
  208.    free(Seg_Array);
  209.    free(Side_Array);
  210.    free(Line_Array);
  211.    free(Node_Array);
  212.    free(PNode_Array);
  213.    free(Sector_Array);
  214.    free(Vertex_Array);
  215.    free(SSector_Array);
  216.    free(Blockmap_Array);
  217.  
  218.    free(screenbuf);
  219. }
  220.  
  221.  
  222. /* This is the main drawing function. */
  223.  
  224. void DrawView(void)
  225. {
  226.    short i;
  227.    long starttime;
  228.  
  229. /* Initialize housekeeping variable for each frame. */
  230.  
  231.    ColCount = 0;
  232.    WallCount = 0;
  233. #if 1
  234.    WallRunCount = 0;
  235. #endif
  236.    FirstSSector = 1;
  237.  
  238. #ifdef COUNT
  239.    drawview++;
  240. #endif
  241. #ifdef PROFILE
  242.    addfloor       = 0;
  243.    addfloor_loops = 0;
  244.    addwall        = 0;
  245.    addwall_loops  = 0;
  246.    coldraw        = 0;
  247.    coldraw_loops  = 0;
  248.    rowdraw        = 0;
  249.    rowdraw_loops  = 0;
  250.    drawnode = 0;
  251.    drawssector = 0;
  252.    drawssector_loops = 0;
  253.    loadseg = 0;
  254.    loadseg_loops1 = 0;
  255.    loadseg_loops2 = 0;
  256.    setuptime = 0;
  257.    drawtime = 0;
  258.    c2ptime = 0;
  259. #endif
  260.  
  261.    for(i = 0;i < width;i++) {
  262. #if 0
  263.       Col_Done[i] = 0;   /* No columns have been drawn. */
  264.       int_count[i] = 0;  /* No walls have been drawn. */
  265.       MinY[i] = 0;       /* Min y value is 0. */
  266. #endif
  267.       MaxY[i] = height;     /* Max y value is 'height' (was 200). */
  268.    }
  269.  
  270.    memset(Col_Done, 0, width * sizeof(char));
  271. #if 1
  272.    memset(int_count, 0, width * sizeof(short));
  273. #endif
  274.    memset(MinY, 0, width * sizeof(short));
  275.  
  276. #if 1
  277.    for (i = 0;i < height;i++) { /* No floors or ceilings have been drawn. */
  278. #if 0
  279.       runcount[i] = 0;
  280. #endif
  281.       floorlist[i][0].end = 0;
  282.       floorlist[i][0].start = width;
  283.       floorlst[i] = -1;
  284.    }
  285. #endif
  286.  
  287.    memset(runcount, 0, height * sizeof(short));
  288.  
  289. /* This is the recursive function.
  290.  * It can probably be rewritten to use data recursion.
  291.  */
  292.  
  293. #ifdef COUNT
  294.    starttime = get_timer();
  295. #endif
  296.  
  297. if ((nofloor && !flooropt) || singlestep || draw_2D)
  298.    memset(screenbuf, 0, (long)width * height);
  299.  
  300.    DrawNode(MaxNode);
  301. #ifdef COUNT
  302.    setuptime += get_timer() - starttime;
  303. #endif
  304.  
  305. /* Draw all of the wall_runs and floor_runs onto the offscreen buffer. */
  306.  
  307. if (!draw_2D) {
  308. #ifdef COUNT
  309.    starttime = get_timer();
  310. #endif
  311.    DrawSegs();
  312. #ifdef COUNT
  313.    drawtime += get_timer() - starttime;
  314. #endif
  315. }
  316.  
  317. singlestep = 0;
  318.  
  319. /* Blast the offscreen buffer to display memory. */
  320.  
  321. #ifdef COUNT
  322.    starttime = get_timer();
  323. #endif
  324.    c2p(screenbuf, width, height);
  325. #ifdef COUNT
  326.    c2ptime += get_timer() - starttime;
  327. #endif
  328.  
  329. #ifdef PROFILE
  330.    printf("Addfloor: %ld %ld\n", addfloor, addfloor_loops);
  331.    printf("Addwall:  %ld %ld\n", addwall, addwall_loops);
  332.    printf("Coldraw:  %ld %ld\n", coldraw, coldraw_loops);
  333.    printf("Rowdraw:  %ld %ld\n", rowdraw, rowdraw_loops);
  334.    printf("Drawnode:     %ld\n", drawnode);
  335.    printf("Drawssector:  %ld %ld\n", drawssector, drawssector_loops);
  336.    printf("Loadseg:      %ld %ld %ld\n", loadseg, loadseg_loops1, loadseg_loops2);
  337.    printf("Setup time:   %ld\n", setuptime / drawview);
  338.    printf("Draw time:    %ld\n", drawtime / drawview);
  339.    printf("C2p time:     %ld\n", c2ptime / drawview);
  340. #endif
  341. }
  342.